package com.hb.guli.service.edu.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.guli.common.base.result.R;
import com.hb.guli.service.edu.entity.form.CourseInfoForm;
import com.hb.guli.service.edu.entity.vo.CoursePublishVo;
import com.hb.guli.service.edu.entity.vo.CourseQueryVo;
import com.hb.guli.service.edu.entity.vo.CourseVo;
import com.hb.guli.service.edu.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author jack
 * @since 2023-06-30
 */
@CrossOrigin
@Api("课程管理")
@RestController
@RequestMapping("/admin/edu/course")
@Slf4j
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation("新增课程")
    @PostMapping("save-course-info")
    public R saveCourseInfo(
            @ApiParam(value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.saveCourseInfoForm(courseInfoForm);
        return R.ok().data("courseId", courseId);
    }

    @ApiOperation("跟据课程 id 查询课程")
    @GetMapping("course-info/{id}")
    public R hetById(   @ApiParam(value = "课程id",required = true) @PathVariable String id){
        CourseInfoForm courseInfoForm = courseService.getCourseInfoById(id);
        if(courseInfoForm != null){
            return R.ok().data("item", courseInfoForm);
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("更新课程")
    @PostMapping("update-course-info")
    public R updateCourseInfo(
            @ApiParam(value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm) {
        courseService.updateCourseInfoForm(courseInfoForm);
        return R.ok().message("修改成功");
    }

    @ApiOperation(value = "课程分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页页数",required = true) @PathVariable Long limit,
                      @ApiParam("课程列表查询对象") CourseQueryVo courseQueryVo) {

        Page<CourseVo> pageModel = courseService.selectPage(page,limit,courseQueryVo);
        List<CourseVo> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "跟据id删除课程", notes = "跟据id对课程进行逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam("课程id") @PathVariable String id) {
        //TODO 删除课程视频

        // 删除课程封面
        courseService.deleteCoverById(id);
        // 删除课程
        boolean result = courseService.removeCourseById(id);
        if(result){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    @ApiOperation("跟据id获取课程发布信息")
    @GetMapping("course-publish/{id}")
    public R getCoursePublishById(@ApiParam(value = "课程ID",required = true)
                                  @PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        if(coursePublishVo != null){
            return R.ok().data("item",coursePublishVo);
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("跟据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(@ApiParam(value = "课程id",required = true)
                               @PathVariable String id){
        boolean result = courseService.publishCourseById(id);
        if(result){
            return R.ok().message("发布成功");
        }else{
            return R.error().message("数据不存在");
        }
    }
}
